<template>
  <div class="right-bottom-wrap">
    <Collapse v-model="collapseValue" accordion simple>
      <Panel name="1">
        <span>{{ infoTitle }}</span>
        <div slot="content" style="height:100%;width:100%;">
          <div class="content-box detail-box" v-if="showDetail">
            <meter-detail
              :currentMeterId="currentMeterId"
              :content="meterDetail"
              :columnNames="meterDetailColumns"
              :currentDate="currentDate"
            />
          </div>
          <div class="content-box pie-box" v-else>
            <h-pie3d
              v-if="!showDetail"
              :defaultOptions="hPie3dOptions"
              :chartData="areaMeterData"
              :shrink="collapseIcon"
            />
          </div>
        </div>
      </Panel>
      <Panel name="2">
        表具寿命诊断报告
        <div class="report-box" slot="content">
          <div class="content">
            <p class="head tc">*****单位燃气表使用寿命诊断报告</p>
            <p class="time tr">--{{ currentDate }}</p>
            <div class="item">
              <div class="sub-tit">表具覆盖范围：</div>
              <p class="text">
                右上角 ({{ diagReport.maxLng }},{{ diagReport.maxLat }})&nbsp;&nbsp;左下角 ({{ diagReport.minLng }},{{
                  diagReport.minLat
                }})
              </p>
            </div>
            <div class="item">
              <div class="sub-tit">数据统计：</div>
              <p class="info">
                统计的总量
                <span>{{ diagReport.count }}</span> 只，平均使用寿命 <span>{{ diagReport.avgYear }}</span> 年，中位数
                <span>{{ diagReport.medianYear }}</span> 年，最大使用寿命
                <span>{{ diagReport.maxYear }}</span>
                年。其中{{ useList }}
              </p>
            </div>
            <div class="item">
              <div class="sub-tit">超期服务表集中区:</div>
              <p class="info-wraning" v-if="firstFive">{{ firstFive }}</p>
            </div>
          </div>
        </div>
      </Panel>
    </Collapse>
  </div>
</template>
<script>
import { each, merge, isEmpty } from 'lodash'
import dayjs from 'dayjs'
import { createNamespacedHelpers } from 'vuex'
import { hPie3dOptions } from './chartOptions'
import meterDetailColumns from './meterDetailColumns'
const { mapState, mapActions } = createNamespacedHelpers('meterManageDestribution')

export default {
  name: 'distribution-right-bottom',
  components: {
    MeterDetail: () => import('@/commons/MeterDetail'),
    HPie3d: () => import('@/commons/charts/HPie3d')
  },
  data() {
    return {
      collapseValue: ['1']
    }
  },
  mounted() {
    this.loadDiagReport()
  },
  computed: {
    ...mapState([
      'chartType',
      'currentMeterId',
      'hPieChartList',
      'meterDetail',
      'defaultDate',
      'diagReport',
      'collapseIcon'
    ]),
    infoTitle() {
      return this.chartType === 'rl' ? '饼状图(区域)' : '详情'
    },
    showDetail() {
      return this.chartType === 'fb'
    },
    hPie3dOptions() {
      return hPie3dOptions
    },
    areaMeterData() {
      const legendData = []
      const _serList = []
      each(this.hPieChartList, item => {
        legendData.push(item.year + '')
        _serList.push({
          name: item.year,
          y: item.count
        })
      })

      const _vis = !isEmpty(_serList)

      return merge({}, hPie3dOptions, {
        series: [
          {
            animation: true,
            data: _serList,
            visible: _vis
          }
        ]
      })
    },
    meterDetailColumns() {
      return meterDetailColumns
    },
    currentDate() {
      return dayjs(this.defaultDate).format('YYYY-MM')
    },
    useList() {
      const { data } = this.diagReport
      const len = data.length
      let str = ''
      each(data, (item, index) => {
        if (index === len - 1) {
          str += `使用 ${item.key} 年 ${item.value} 只。`
        } else {
          str += `使用 ${item.key} 年 ${item.value} 只、`
        }
      })
      return str
    },
    firstFive() {
      const { firstFive } = this.diagReport
      const len = firstFive.length
      let str = ''
      each(firstFive, (item, index) => {
        if (index === len - 1) {
          str += `${item.address}。`
        } else {
          str += `${item.address}、`
        }
      })
      return str
    }
  },
  methods: {
    ...mapActions(['loadDiagReport'])
  }
}
</script>
<style lang="less" scoped>
@deep: ~'>>>';

.right-bottom-wrap {
  width: 100%;
  height: 100%;

  .report-box {
    padding: 10px 16px;
    max-height: 150px;
    box-sizing: border-box;
    overflow-y: auto;

    .item {
      margin-bottom: 10px;

      .sub-tit {
        font-size: 15px;
      }

      p {
        text-indent: 2em;
      }
    }
  }

  .detail-box {
    padding: 0 10px;
    line-height: 1.5;
    box-sizing: border-box;
    overflow-y: auto;
  }

  .pie-box {
    padding: 10px 0;
    width: 100%;
    height: 150px;
    overflow: hidden;
    box-sizing: border-box;
  }

  @{deep} .ivu-collapse {
    display: flex;
    flex-direction: column;
    height: 100%;

    .ivu-collapse-header {
      padding-left: 10px;
      i {
        margin-right: 0;
      }
    }

    .ivu-collapse-item-active {
      flex: 1;
      display: flex;
      flex-direction: column;
    }

    .ivu-collapse-content {
      padding: 0;

      .ivu-collapse-content-box {
        position: relative;
        padding: 0;
        height: 100%;
      }
    }

    .ivu-collapse-item {
      margin-top: 10px;
      overflow: hidden;
    }
  }
}
</style>
